package com.ntschy.web.controller.sys;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ntschy.common.constant.HttpStatus;
import com.ntschy.common.core.AjaxPageResult;
import com.ntschy.common.core.AjaxResult;
import com.ntschy.common.core.controller.BaseController;
import com.ntschy.common.core.entity.dataobject.SysDictData;
import com.ntschy.common.core.entity.dataobject.SysDictType;
import com.ntschy.sys.entity.dto.dicttype.SysDictTypeAddUpdateDTO;
import com.ntschy.sys.entity.dto.dicttype.SysDictTypePageDTO;
import com.ntschy.sys.entity.mapstruct.SysDictTypeMapstruct;
import com.ntschy.sys.entity.valid.SysDictTypeGroups;
import com.ntschy.sys.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * springboot 参数校验
 * mapstruct
 * @author ezreal_geng
 */
@RestController
@RequestMapping("/sys/dictType")
@Api(tags = "字典类型接口")
@Validated
public class SysDictTypeController extends BaseController<SysDictType> {

    @Autowired
    private ISysDictTypeService dictTypeService;

    // * ================================================基础增删改查BEGIN============================================= *
    @ApiOperation(value = "字典类型分页查询", notes = "")
    @ApiImplicitParam(name = "pageDTO", value = "字典类型分页查询对象", required = true, dataType = "SysDictTypePageDTO")
    //@PreAuthorize("@ss.hasPermi('sys:dictType:page')")
    @PostMapping("/pageDictType")
    public AjaxPageResult<SysDictType> pageDictType(@RequestBody SysDictTypePageDTO pageDTO) {
        Page<SysDictType> page = PageHelper.startPage(pageDTO.getCurrPage(), pageDTO.getPageSize());
        SysDictType dictType = SysDictTypeMapstruct.SYS_DICT_TYPE_MAP_STRUCT.pageDtoToDo(pageDTO);
        List<SysDictType> dictTypeList = dictTypeService.pageDictType(dictType);
        return buildPageResult(dictTypeList, page);
    }

    @ApiOperation(value = "新增字典类型", notes = "")
    @ApiImplicitParam(name = "addDto", value = "字典类型新增实体类", required = true, dataType = "SysDictTypeAddUpdateDTO")
    //@PreAuthorize("@ss.hasPermi('sys:dictType:add')")
    @PostMapping("/addDictType")
    public AjaxResult<String> addDictType(@Validated(SysDictTypeGroups.Add.class) @RequestBody SysDictTypeAddUpdateDTO addDto) {
        if(dictTypeService.checkExistDictName(addDto.getDictName())) {
            return AjaxResult.error(HttpStatus.ERROR, "字典名称已存在");
        } else if(dictTypeService.checkExistDictType(addDto.getDictType())) {
            return AjaxResult.error(HttpStatus.ERROR, "字典类型已存在");
        }
        SysDictType addDictType = SysDictTypeMapstruct.SYS_DICT_TYPE_MAP_STRUCT.addUpdateDtoToDo(addDto);
        return toAjax(dictTypeService.addDictType(addDictType), "新增成功", "新增失败");
    }

    @ApiOperation(value = "更新字典类型", notes = "")
    @ApiImplicitParam(name = "updateDto", value = "字典类型更新实体类", required = true, dataType = "SysDictTypeAddUpdateDTO")
    //@PreAuthorize("@ss.hasPermi('sys:dictType:update')")
    @PostMapping("/updateDictType")
    public AjaxResult<String> updateDictType(@Validated(SysDictTypeGroups.Update.class) @RequestBody SysDictTypeAddUpdateDTO updateDto) {
        if(dictTypeService.checkExistDictName(updateDto.getDictName())) {
            return AjaxResult.error(HttpStatus.ERROR, "字典名称已存在");
        } else if(dictTypeService.checkExistDictType(updateDto.getDictType())) {
            return AjaxResult.error(HttpStatus.ERROR, "字典类型已存在");
        }
        SysDictType updateDictType = SysDictTypeMapstruct.SYS_DICT_TYPE_MAP_STRUCT.addUpdateDtoToDo(updateDto);
        return toAjax(dictTypeService.updateDictType(updateDictType), "更新成功", "更新失败");
    }

    @ApiOperation(value = "根据字典类型ID删除字典类型", notes = "")
    @ApiImplicitParam(name = "dictTypeId", value = "字典类型ID", required = true, dataType = "String")
    //@PreAuthorize("@ss.hasPermi('sys:dictType:delete')")
    @GetMapping("/deleteDictType")
    public AjaxResult<String> deleteDictType(@NotBlank(message = "字典类型ID不能为空") @Length(min = 36, max = 36, message = "字典类型ID不合法") String dictTypeId) {
        if(dictTypeService.checkDictTypeIsSystem(dictTypeId)) {
            return AjaxResult.error(HttpStatus.ERROR, "系统内置字典类型不允许删除");
        } else if(dictTypeService.checkExistDictData(dictTypeId)) {
            return AjaxResult.error(HttpStatus.ERROR, "字典类型已使用不允许删除");
        }
        return toAjax(dictTypeService.deleteDictType(dictTypeId), "删除角字典类型成功", "删除字典类型失败");
    }

    // * =================================================基础增删改查END============================================== *

    @ApiOperation(value = "根据字典类型获取所有字典数据", notes = "")
    @ApiImplicitParam(name = "dictType", value = "字典类型", required = true, dataType = "String")
    //@PreAuthorize("@ss.hasPermi('sys:dictData:list')")
    @GetMapping("/listDictDataByDictType")
    public AjaxResult<List<SysDictData>> listDictDataByDictType(@NotBlank(message = "字典类型不能为空") String dictType) {
        List<SysDictData> dictDataList = dictTypeService.listDictDataByDictType(dictType);
        return AjaxResult.success(dictType + "全部字典数据", dictDataList);
    }

    //
}
